package com.codi.fundData.service.impl;

import com.codi.base.dataSource.annotation.DataSource;
import com.codi.base.util.ListUtil;
import com.codi.bus.constant.GlobalConstant;
import com.codi.fundData.dao.FundKeyStockPortfolioDao;
import com.codi.fundData.dao.HKSecuMainDao;
import com.codi.fundData.dao.SecurityProductDao;
import com.codi.fundData.domain.FundKeyStockPortfolio;
import com.codi.fundData.domain.HKSecuMain;
import com.codi.fundData.domain.SecurityProduct;
import com.codi.fundData.service.FundKeyStockPortfolioService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 模块名
 *
 * @author spy
 * @version 1.0 2017-11-23 9:49
 * @since 1.0
 */
@Slf4j
@Service
@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
public class FundKeyStockPortfolioServiceImpl implements FundKeyStockPortfolioService {

    @Autowired
    private FundKeyStockPortfolioDao fundKeyStockPortfolioDao;

    @Autowired
    private SecurityProductDao securityProductDao;

    @Autowired
    private HKSecuMainDao hkSecuMainDao;


    @Override
    @DataSource(GlobalConstant.DATA_SOURCE_FUND_DETAIL)
    public List<Map<String, Object>> qry(int innerCode) {

        List<FundKeyStockPortfolio> stocks = fundKeyStockPortfolioDao.selectLatests(innerCode);

        if (ListUtil.isEmpty(stocks)) {
            return null;
        }

        List<Integer> codes   = new ArrayList<>();
        List<Integer> hkCodes = new ArrayList<>();


        for (FundKeyStockPortfolio item : stocks) {
            Integer code = item.getStockInnerCode();
            if (code < 1000000) {
                codes.add(code);
            } else if (code > 1000000 && code < 2000000) {
                hkCodes.add(code);
            }
        }


        // <1000000 secuMain
        List<SecurityProduct>        products = null;
        Map<String, SecurityProduct> mapping  = null;
        if (!codes.isEmpty()) {
            products = securityProductDao.querySecurityProductsByInnerCodes(codes);
            if (products != null && !products.isEmpty()) {
                mapping = new HashedMap();
                for (SecurityProduct item : products) {
                    mapping.put("" + item.getInnerCode().intValue(), item);
                }
            }
        }


        // 1000000<<2000000 hk secu main
        List<HKSecuMain>        hkProducts = null;
        Map<String, HKSecuMain> hkMapping  = null;
        if (!hkCodes.isEmpty()) {
            hkProducts = hkSecuMainDao.queryByInnerCodes(hkCodes);
            if (hkProducts != null && !hkProducts.isEmpty()) {
                hkMapping = new HashedMap();
                for (HKSecuMain item : hkProducts) {
                    hkMapping.put("" + item.getInnercode().intValue(), item);
                }
            }
        }


        List<Map<String, Object>> ret = new ArrayList<>();
        for (FundKeyStockPortfolio item : stocks) {
            Integer code = item.getStockInnerCode();

            Map<String, Object> map = new HashedMap();
            map.put("ReportDate", item.getReportDate());
            map.put("RatioInNV", item.getRatioInNV());
            map.put("SerialNumber", item.getSerialNumber());
            map.put("HoldVolume", item.getSharesHolding());
            map.put("MarketValue", item.getMarketValue());

            if (code < 1000000) {
                if (mapping != null) {
                    SecurityProduct product = mapping.get("" + code.intValue());
                    if (product != null) {
                        map.put("FundName", product.getChiNameAbbr());
                        map.put("FundNameAbbr", product.getSecuAbbr());
                    }
                }
            } else if (code > 1000000 && code < 2000000) {
                if (hkMapping != null) {
                    HKSecuMain product = hkMapping.get("" + code.intValue());
                    if (product != null) {
                        map.put("FundName", product.getChiname());
                        map.put("FundNameAbbr", product.getSecuabbr());
                    }
                }
            }
            ret.add(map);
        }

        return ret;
    }
}
